package com.user.entity.dto;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;

import com.user.entity.Department;
import com.user.entity.Student;

public class StudentDTO {
	private Long id;
	private String name;
	private String num;
	private String sex;
	private Integer grade;
	private String department;
	private String password;
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getNum() {
		return num;
	}
	public void setNum(String num) {
		this.num = num;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Integer getGrade() {
		return grade;
	}
	public void setGrade(Integer grade) {
		this.grade = grade;
	}

	public String getDepartment() {
		return department;
	}
	public void setDepartment(String department) {
		this.department = department;
	}
	public StudentDTO() {
		super();
	}
	public StudentDTO(Student student,String department) {
		super();
		if(student!=null) {
			this.id = student.getId();
			this.name = student.getName();
			this.num = student.getNum();
			this.sex = student.getSex();
			this.grade = student.getGrade();
			this.password = student.getPassword();
			this.department = department;
		}
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public static Specification<Student> getSpecification(String str,Long dept_id) {
		Specification<Student> spec = new Specification<Student>() {
			public Predicate toPredicate(Root<Student> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				
				List<Predicate> list = new ArrayList<Predicate>();
				List<Predicate> result_list = new ArrayList<Predicate>();
				Predicate[] p = new Predicate[list.size()];
				if(null!=str&&!"".equals(str)) {
					Predicate  predicate1 =  cb.like(root.get("name").as(String.class),"%"+str+"%");
					list.add(predicate1);
					Predicate  predicate2 =  cb.like(root.get("num").as(String.class),"%"+str+"%");
					list.add(predicate2);

					Predicate  result =  cb.or(list.toArray(p));
					result_list.add(result);
				}
				if(null!=dept_id) {
					Join<Student, Department> join = root.join(root.getModel().getSingularAttribute("department",Department.class),JoinType.LEFT);
					Predicate  predicate =  cb.equal(join.get("id").as(Long.class),dept_id);
					result_list.add(predicate);
				}
				
				return cb.and(result_list.toArray(p));
			}
		};
		return spec;
	}
}
